package com.cnxjs.shs4et.repository;

import com.cnxjs.shs4et.entity.Topic;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface TopicRepository extends JpaRepository<Topic, Long> {
    // 按照具体板块分页查所有帖子
    List<Topic> findAllByForum(Pageable pageable, Integer forumId);

    // 按照论坛分页查所有帖子
    @Query(value = "SELECT t.* FROM topic t WHERE t.forum IN (SELECT f.id FROM forum f WHERE f.name = ?4) ORDER BY CASE WHEN ?3 = 'asc' THEN t.id END ASC, CASE WHEN ?3 = 'desc' THEN t.id END DESC LIMIT ?2 OFFSET ?1", nativeQuery = true)
    List<Topic> findAllByForumName(Integer offset, Integer size, String sortRule, String name, Integer page);

    // 按照Id查帖子
    Topic  findById(Integer id);

    // 按照标题模糊查询
    List<Topic> findAllByTitleLike(Pageable pageable, String title);

    // 按照具体板块分页查所有不被锁定的帖子
    List<Topic> findAllByIsLocked(Pageable pageable, int isLocked);
}
